package com.shouer.doc.ui.neworder.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shouer.doc.R;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.DateFormatUtil;
import com.shouer.doc.common.util.RelativeDateFormatUtil;
import com.shouer.doc.ui.neworder.model.SimpleOrderModel;
import com.shouer.doc.ui.neworder.view.INewOrdView;
import com.shouer.doc.ui.ordermanager.adapter.OrderInOrderListAdapter;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ZXL on 2016/11/25.
 */

public class NewOrderListAdapter extends RecyclerView.Adapter<NewOrderViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<SimpleOrderModel> mOrderModels;
    private LayoutInflater layoutInflater;
    private INewOrdView newOrdView;

    public NewOrderListAdapter(Context context, List<SimpleOrderModel> orderModels, INewOrdView newOrdView) {
        this.mContext = context;
        this.mOrderModels = orderModels;
        layoutInflater = LayoutInflater.from(context);
        this.newOrdView = newOrdView;
    }

    @Override
    public NewOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.fragment_neworder_item_layout, null);
        return new NewOrderViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(NewOrderViewHolder holder, int position) {

        SimpleOrderModel simpleOrderModel = mOrderModels.get(position);
        //挂号送药
        if (simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_YAO) {
            String icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_yao;
            Uri uri = Uri.parse(icoUriString);
            holder.patIcoView.setImageURI(uri);
        } else if (simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_TUWEN) {
            String icoUriString=null;
            if (simpleOrderModel.getChannel()==7)
            {
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_list_quickask;
            }else if (simpleOrderModel.getChannel()==1){
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_free;
            } else
            {
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_tw;
            }
            Uri uri = Uri.parse(icoUriString);
            holder.patIcoView.setImageURI(uri);
        } else {
            String icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_audio;
            Uri uri = Uri.parse(icoUriString);
            holder.patIcoView.setImageURI(uri);
        }
        if (simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_YAO) {
            holder.patNameView.setText(simpleOrderModel.getPatient().getPatName());
            holder.patAgeView.setVisibility(View.INVISIBLE);
            holder.patGenderView.setVisibility(View.INVISIBLE);

            try {
                holder.timeView.setText(RelativeDateFormatUtil.format(DateFormatUtil.stringToDate(simpleOrderModel.getCreateTime().replace("T", "\b"), "yyyy-MM-dd\bHH:mm:ss")));
            } catch (Exception e) {
                Log.d(NewOrderListAdapter.class.getSimpleName(), e.toString());
                holder.timeView.setText("");
            }

            holder.patDescribe.setText(mContext.getResources().getString(R.string.ORDER_NAME_YAO));
            holder.orderInfoLayout.setVisibility(View.GONE);
            holder.patDescribe.setVisibility(View.VISIBLE);

            holder.refuseBtn.setVisibility(View.GONE);
            holder.actionView.setVisibility(View.VISIBLE);

        } else {

            holder.patNameView.setText(simpleOrderModel.getPatient().getPatName());
            holder.patNameView.setVisibility(View.VISIBLE);
            int age = 0;
            try {
                age = DateFormatUtil.getAgeByBirthday(DateFormatUtil.stringToDate(simpleOrderModel.getPatient().getPatBirthday().replace("T", "\b"), "yyyy-MM-dd"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            age = age < 0 ? 0 : age;
            holder.patAgeView.setText(mContext.getResources().getString(R.string.patient_age, age));
            holder.patAgeView.setVisibility(View.VISIBLE);
            holder.patGenderView.setText(simpleOrderModel.getPatient().getPatSex() == 0 ? "女" : "男");
            holder.patGenderView.setVisibility(View.VISIBLE);
            try {
                holder.timeView.setText(RelativeDateFormatUtil.format(DateFormatUtil.stringToDate(simpleOrderModel.getCreateTime().replace("T", "\b"), "yyyy-MM-dd\bHH:mm:ss")));
            } catch (Exception e) {
                Log.d(NewOrderListAdapter.class.getSimpleName(), e.toString());
                holder.timeView.setText("");
            }
            if (simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_TUWEN) {
                holder.patDescribe.setText(simpleOrderModel.getPatDescribe());
                holder.orderInfoLayout.setVisibility(View.GONE);
                holder.patDescribe.setVisibility(View.VISIBLE);
            } else {

                try {
                    Date createDate= DateFormatUtil.stringToDate(simpleOrderModel.getScheduleTime().replace("T","\b"),"yyyy-MM-dd\bHH:mm:ss");
                    holder.orderTimeView.setText(DateFormatUtil.dateToString(createDate,"yyyy-MM-dd HH:mm"));
                } catch (ParseException e) {
                    Log.e(OrderInOrderListAdapter.class.getSimpleName(),e.toString());
                    holder.orderTimeView.setText("-");
                }
                holder.orderDurationView.setText(mContext.getResources().getString(R.string.order_duration, simpleOrderModel.getOrderTime()));
                if (simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_DIANHUA) {
                    holder.telView.setText(simpleOrderModel.getContactTel());
                    holder.telLayout.setVisibility(View.VISIBLE);
                    holder.orderTypeView.setImageResource(R.drawable.ico_order_type_audio);
                } else {
                    holder.telLayout.setVisibility(View.GONE);
                    holder.orderTypeView.setImageResource(R.drawable.ico_order_type_video);
                }
                holder.patDescribe.setVisibility(View.GONE);
                holder.orderInfoLayout.setVisibility(View.VISIBLE);

            }

            if (TextUtils.isEmpty(simpleOrderModel.getDocId())) {
                holder.refuseBtn.setVisibility(View.GONE);
            } else {
                holder.refuseBtn.setVisibility(View.VISIBLE);
            }
        }

        holder.itemView.setTag(R.id.list_position, position);
        holder.actionView.setTag(R.id.list_position, position);
        holder.refuseBtn.setTag(R.id.list_position, position);
        holder.itemView.setOnClickListener(this);
        holder.actionView.setOnClickListener(this);
        holder.refuseBtn.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {
        if (null == mOrderModels) {
            return 0;
        } else {
            return mOrderModels.size();
        }
    }

    public void refreshDocList(List<SimpleOrderModel> orderModels) {
        Log.i(NewOrderListAdapter.class.getSimpleName(), "refreshDocList");
        this.mOrderModels = orderModels;
        notifyDataSetChanged();
    }

    public void attachToDocList(List<SimpleOrderModel> orderModels) {
        Log.i(NewOrderListAdapter.class.getSimpleName(), "attachToDocList");
        if (null != orderModels) {
            if (null == mOrderModels) {
                mOrderModels = new ArrayList<>(0);
            }

            mOrderModels.addAll(orderModels);
            notifyDataSetChanged();
        }
    }

    public void removeOrder(SimpleOrderModel simpleOrderModel) {
        try {
            mOrderModels.remove(simpleOrderModel);
            notifyDataSetChanged();
        } catch (Exception e) {
            Log.e(NewOrderListAdapter.class.getSimpleName(), "removeOrder Exception:" + e.toString());
        }
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.order_item:
                int position = (int) view.getTag(R.id.list_position);
                SimpleOrderModel simpleOrderModel = mOrderModels.get(position);
                newOrdView.newOrderItemClick(simpleOrderModel);
                break;
            case R.id.action_btn:
                int actionPosition = (int) view.getTag(R.id.list_position);
                SimpleOrderModel orderModel = mOrderModels.get(actionPosition);
                newOrdView.receiveOrder(orderModel);
                break;
            case R.id.refuse_btn:
                int refusePosition = (int) view.getTag(R.id.list_position);
                SimpleOrderModel refuseOrderModel = mOrderModels.get(refusePosition);
                newOrdView.refuseOrder(refuseOrderModel);
                break;
        }

    }
}
